package com.qall.media;


import com.alibaba.fastjson.parser.Feature;
import com.google.common.collect.HashBiMap;
import com.qall.media.entity.RtspPlayEntity;
import com.qall.util.StringUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.xml.crypto.Data;
import java.io.*;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;

@Controller
public class MediaController {

    @Value("${opencv.pic.prefix}")
    private String picPrefix;
    @Resource
    private RtspProcess process;

    ExecutorService executorService= Executors.newCachedThreadPool();


    @PostMapping("/uploadImg")
    @ResponseBody
    public void uploadImg(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        fileMap.entrySet().forEach(x -> {
            String filePath = getFileName(request.getSession());
            File file = new File(filePath);
            try {
                OutputStream outputStream = new FileOutputStream(file);
                x.getValue().getName();
                InputStream stream = x.getValue().getInputStream();
                byte[] bytes = new byte[1024];
                while (stream.read(bytes) != -1) {
                    outputStream.write(bytes);
                }
                outputStream.close();
                stream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    @GetMapping("/getImage")
    @ResponseBody
    public void getImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        File file = new File(getFileName(request.getSession()));
        if (file == null) {
            return;
        }
        InputStream stream = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        byte[] bytes = new byte[516];
        while (stream.read(bytes) != -1) {
            out.write(bytes);
        }
        response.setContentType("img/png");
        out.flush();
        out.close();
        out.close();
        stream.close();
    }


    @PostMapping("playRtsp")
    @ResponseBody
    public String playRtsp(@RequestBody RtspPlayEntity entity) {

        String time = RandomStringUtils.randomAlphabetic(5).toLowerCase(Locale.ROOT);

        String rtmp = "rtmp://localhost:10004/hls/"+time ;



        Runnable runnable= () -> process.rtspToRtmp(entity.getUrl(), rtmp, (frame) -> frame);
       executorService.submit(runnable);

        return time;
    }


    private String getFileName(HttpSession session) {
        return picPrefix + File.pathSeparatorChar + session.getId();
    }
}
